﻿using System;
using System.Collections.Generic;
using Commons.Collections;

using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;

namespace Joyintech.WinService.inpiao365
{
    /// <summary>
    /// 分页
    /// </summary>
    public class PageSplit
    {
        /// <summary>
        /// 计算总页数
        /// </summary>
        /// <param name="count">总记录数</param>
        /// <param name="pagesize">每页记录数</param>
        /// <returns>总页数</returns>
        public int GetPages(int count, int pagesize)
        {
            if (count % pagesize == 0)
                return count / pagesize;
            else
                return count / pagesize + 1;
        }

        /// <summary>
        /// 获取上一页页码
        /// </summary>
        /// <param name="current">当前页</param>
        /// <returns></returns>
        public int GetPagePrev(int current,int pages)
        {
            if (current > 1 && current < pages)
                return current - 1;
            else if (current > pages)
                return pages - 1;
            else
                return 1;
        }


        /// <summary>
        /// 获取下一页页码
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <returns></returns>
        public int GetPageNext(int current,int pages)
        {
            if (current < pages)
                return current + 1;
            else
                return pages;
        }

        /// <summary>
        /// 分页条显示开始页
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <param name="pagenumbers">显示几页</param>
        /// <returns></returns>
        public int GetPageFirst(int current, int pages, int pagenumbers)
        {
            int mid = pagenumbers / 2;
            if (pages <= pagenumbers)
                return 1;
            else
            {
                int first = (current - mid) > 1 ? (current - mid) : 1;
                return first>(pages-pagenumbers + 1)?(pages-pagenumbers + 1):first ;
            }
        }

        /// <summary>
        /// 分页条显示结束页
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pages">总页数</param>
        /// <param name="pagenumbers">显示几页</param>
        /// <returns></returns>
        public int GetPageLast(int current, int pages, int pagenumbers)
        {
            int mid = pagenumbers / 2;
            if (pages <= pagenumbers)
                return pages;
            else
            {
                int last = ((current+mid) < pagenumbers) ? pagenumbers : (current + mid);
                return last<pages?last:pages;
            }
        }

        /// <summary>
        /// 获取某个分页的静态页面地址
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="current"></param>
        /// <returns></returns>
        public string GetFileName(string filename, int current)
        {
            return filename + "_" + current + ".htm";
        }

        /// <summary>
        /// 设置分页参数
        /// </summary>
        /// <param name="vh"></param>
        /// <param name="table">表名称</param>
        /// <param name="query">查询条件</param>
        /// <param name="current">当前页码</param>
        public void SetPage(VelocityHelper vh, int current, string filename, int pageCount)
        {
            vh.Put("filename", filename);//分页链接的文件名前缀
            vh.Put("current", current);//设置当前页面索引，从1开始计数
            vh.Put("page", pageCount);//设置总页数
            //vh.Put("count", 100);//测试
            vh.Put("pager", new PageSplit());
        }
    }
}
